unit Rap_MalAlimArizaAnalizAyrinti;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls, ppBands, ppClass, ppCtrls, ppVar,
  ppPrnabl, ppCache, ppProd, ppReport, ppComm, ppRelatv, ppDB, ppDBPipe,
  DB, ADODB, Grids, Wwdbigrd, Wwdbgrid, Mask, wwdbedit, Wwdotdot,
  wwdbdatetimepicker, ppStrtch, ppSubRpt, ppModule, raCodMod, ppParameter,
  Wwdbcomb, TXComp, TXRB;

type
  TRap_MalAlimArizaAnalizAyrintiF = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    bastarihk: TwwDBDateTimePicker;
    bittarihk: TwwDBDateTimePicker;
    StokKoduk: TwwDBComboDlg;
    Panel2: TPanel;
    blistele: TBitBtn;
    bYazdir: TBitBtn;
    bkapat: TBitBtn;
    wwDBGrid1: TwwDBGrid;
    RaporQ: TADOQuery;
    RaporQS: TDataSource;
    ppDBPipeline1: TppDBPipeline;
    ppReport1: TppReport;
    RaporAyrintiQS: TDataSource;
    ppDBPipelineAyrint1: TppDBPipeline;
    RaporAyrintiQ: TADOQuery;
    ppParameterList1: TppParameterList;
    Label5: TLabel;
    FirmaAdik: TwwDBComboDlg;
    ayrinti1: TMemo;
    ayrinti2: TMemo;
    Label6: TLabel;
    islemk: TwwDBComboBox;
    ppTitleBand1: TppTitleBand;
    ppLabel1: TppLabel;
    ppHeaderBand1: TppHeaderBand;
    ppLabel2: TppLabel;
    ppLabel3: TppLabel;
    ppLabel5: TppLabel;
    ppLabel6: TppLabel;
    ppLabel7: TppLabel;
    ppLabel8: TppLabel;
    ppLine1: TppLine;
    ppDetailBand1: TppDetailBand;
    ppSubReport1: TppSubReport;
    ppChildReport1: TppChildReport;
    ppTitleBand2: TppTitleBand;
    ppHeaderBand2: TppHeaderBand;
    ppDetailBand2: TppDetailBand;
    ppDBText3: TppDBText;
    ppDBText8: TppDBText;
    ppVariable1: TppVariable;
    ppSummaryBand2: TppSummaryBand;
    raCodeModule1: TraCodeModule;
    ppFooterBand1: TppFooterBand;
    ppLine2: TppLine;
    ppSystemVariable1: TppSystemVariable;
    ppSystemVariable2: TppSystemVariable;
    ppSummaryBand1: TppSummaryBand;
    ppShape1: TppShape;
    ppDBCalc5: TppDBCalc;
    ppDBCalc6: TppDBCalc;
    ppDBCalc7: TppDBCalc;
    ppDBCalc8: TppDBCalc;
    ppLabel4: TppLabel;
    ppGroup1: TppGroup;
    ppGroupHeaderBand1: TppGroupHeaderBand;
    ppDBText2: TppDBText;
    ppDBText4: TppDBText;
    ppDBText5: TppDBText;
    ppDBText6: TppDBText;
    ppDBText7: TppDBText;
    ppDBText1: TppDBText;
    ppLabel11: TppLabel;
    ppLabel10: TppLabel;
    ppLabel9: TppLabel;
    ppLine3: TppLine;
    ppGroupFooterBand1: TppGroupFooterBand;
    raCodeModule2: TraCodeModule;
    ExtraOptions1: TExtraOptions;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure bkapatClick(Sender: TObject);
    procedure blisteleClick(Sender: TObject);
    procedure StokKodukKeyPress(Sender: TObject; var Key: Char);
    procedure bYazdirClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FirmaAdikKeyPress(Sender: TObject; var Key: Char);
    procedure FirmaAdikExit(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Rap_MalAlimArizaAnalizAyrintiF: TRap_MalAlimArizaAnalizAyrintiF;
  CariKod: integer;

implementation
uses data, UnitGenel;
{$R *.dfm}

procedure TRap_MalAlimArizaAnalizAyrintiF.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := CaFree;
  Rap_MalAlimArizaAnalizAyrintiF := nil;
end;

procedure TRap_MalAlimArizaAnalizAyrintiF.bkapatClick(Sender: TObject);
begin
  close;
end;

procedure TRap_MalAlimArizaAnalizAyrintiF.blisteleClick(Sender: TObject);
begin
  RaporQ.close;
  RaporQ.sql.text := 'select stokkod,stokkod as stokkod1,stokkodu=dbo.getstokkodu(stokkod),tarih=convert(varchar(10),(select tarih from fatura f where f.bilisno=fathar.bilisno),104),';
  RaporQ.sql.add('bilisno,bilisno as bilisno1,sum(miktar) as miktar,');
  RaporQ.sql.add('satilanadet=(select count(*) from fatharhar f where f.bilisnogir=fathar.bilisno and f.stokkod=fathar.stokkod and bilisnocik is not null),');
  RaporQ.sql.add('tumarizaliadet=(select count(*) from onarim o where o.serino in (select serinos from fatharhar f where f.bilisnogir=fathar.bilisno and f.stokkod=fathar.stokkod) and (select carikodgir from teknik where teknik.formno=o.formno)<>12810),');
  RaporQ.sql.add('degisenadet=(select count(*) from onarim o where o.serino in (select serinos from fatharhar f where f.bilisnogir=fathar.bilisno and f.stokkod=fathar.stokkod) and yserino is not null and (select carikodgir from teknik where teknik.formno=o.formno)<>12810)');
  RaporQ.sql.add('into #yeni from fathar where 1=1 ');

  if Stokkoduk.text <> '' then
    RaporQ.sql.add('and stokkod in (select stokkod from stok where stokkodu like ' + #39 + Stokkoduk.text + #39 + ')');

  if FirmaAdik.text <> '' then
    RaporQ.sql.add('and (select carikod from fatura f where f.bilisno=fathar.bilisno)=' + inttostr(CariKod));

  if bastarihk.Text <> '' then
    RaporQ.sql.add('and (select tarih from fatura f where f.bilisno=fathar.bilisno)>=' + #39 + bastarihk.Text + #39);

  if bittarihk.Text <> '' then
    RaporQ.sql.add('and (select tarih from fatura f where f.bilisno=fathar.bilisno)<=' + #39 + bittarihk.Text + #39);

  RaporQ.sql.add(' and (select faturatipi from fatura f where f.bilisno=fathar.bilisno)=1 and (select depokod from fatura f where f.bilisno=fathar.bilisno) in (1,2) ');
  RaporQ.sql.add('and exists(select serinos from fatharhar f where f.bilisnogir=fathar.bilisno) and ');
  RaporQ.sql.add('miktar>50');
  RaporQ.sql.add('group by bilisno,stokkod order by stokkodu,bilisno');
  RaporQ.sql.add('select *,arizaoran=(case satilanadet when 0 then 0 else cast(cast(tumarizaliadet as decimal)/cast(satilanadet as decimal) as decimal(10,3)) end) from #yeni');
  RaporQ.sql.add('drop table #yeni');
  RaporQ.open;

  RaporAyrintiQ.close;
  if islemk.value = '1' then
    RaporAyrintiQ.SQL := ayrinti1.lines
  else
    RaporAyrintiQ.SQL := ayrinti2.lines;
  RaporAyrintiQ.open;

end;

procedure TRap_MalAlimArizaAnalizAyrintiF.StokKodukKeyPress(
  Sender: TObject; var Key: Char);
var
  Bilgi: string;
  StokKod: integer;
  Stokkodu: string[25];
begin
  if key = #13 then
  begin
    Bilgi := StokBul(Sender, 0, 'G');

    if Bilgi = '' then
      exit;

    if Bilgi <> '0' then
    begin
      Stokkod := Strtoint(Copy(Bilgi, 1, Pos(',', Bilgi) - 1));
      Delete(Bilgi, 1, Pos(',', Bilgi));
      Stokkodu := Copy(Bilgi, 1, Pos(',', Bilgi) - 1);

      Stokkoduk.text := Stokkodu;
    end
    else
    begin
      Stokkoduk.text := '';
    end;
  end;

end;

procedure TRap_MalAlimArizaAnalizAyrintiF.bYazdirClick(Sender: TObject);
begin
  ppReport1.print;
end;

procedure TRap_MalAlimArizaAnalizAyrintiF.FormShow(Sender: TObject);
begin
  bittarihk.date := SadeceTarih();
  islemk.ItemIndex := 0;
end;

procedure TRap_MalAlimArizaAnalizAyrintiF.FirmaAdikKeyPress(
  Sender: TObject; var Key: Char);
begin
  if key = #13 then blistele.setfocus;
end;

procedure TRap_MalAlimArizaAnalizAyrintiF.FirmaAdikExit(Sender: TObject);
var
  Bilgi: string;
  Carikodu: string[25];
  FirmaAdi: string[50];
begin
  Bilgi := CariBul(Sender);

  if Bilgi = '' then
    exit;

  if Bilgi <> '0' then
  begin
    Carikod := Strtoint(Copy(Bilgi, 1, Pos(',', Bilgi) - 1));
    Delete(Bilgi, 1, Pos(',', Bilgi));
    Carikodu := Copy(Bilgi, 1, Pos(',', Bilgi) - 1);
    Delete(Bilgi, 1, Pos(',', Bilgi));
    FirmaAdi := Bilgi;

    FirmaAdik.text := FirmaAdi;
  end
  else
  begin
    CariKod := 0;
    FirmaAdik.text := '';
  end;
end;

end.

